﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;

namespace DuJianliang.ApplicationPlatform.GetRepair
{
    public partial class DispatchMissileSelect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindDispatchList();
                BindGridView();
            }
        }

        private void BindDispatchList()
        {
            // 查询出已下达但未完工的派工单
            string sql = @"SELECT FlowInstanceID, DispatchCode FROM dbo.t5715_Dispatch WHERE IssueState = 1 AND (DispatchState <> 1 OR DispatchState IS NULL) ORDER BY CreateDate DESC";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

                ddlDispatchList.Items.Clear();
                ddlDispatchList.DataSource = dataTable;
                ddlDispatchList.DataTextField = "DispatchCode";
                ddlDispatchList.DataValueField = "FlowInstanceID";
                ddlDispatchList.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void BindGridView()
        {
            if (string.IsNullOrWhiteSpace(ddlDispatchList.SelectedValue))
            {
                return;
            }

            //string sql = "SELECT *,1 AS [Count] FROM dbo.t5715_DispatchMissile WHERE DispatchID = @DispatchID";

            // 已完成审批的领修单中包含的导弹不能被再次选择
            string sql = @"SELECT *,1 AS [Count] FROM dbo.t5715_DispatchMissile WHERE DispatchID = @DispatchID
                            AND DispatchMissileID NOT IN (
                            SELECT DispatchMissileID FROM dbo.t5715_GetRepairDetail WHERE GetRepairID IN
                            (SELECT GR.FlowInstanceID FROM dbo.t5715_GetRepair GR
                            INNER JOIN dbo.FlowInstance FI ON GR.FlowInstanceID = FI.FlowInstanceID AND FI.Completed = 1 AND FI.OperationID = 2))";
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchID", DbType.String, ddlDispatchList.SelectedValue);
                DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];
                gridView.DataSource = dataTable;
                gridView.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void ddlDispatchList_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(e.CommandName) || e.CommandName.ToLower().Equals("page"))
            {
                return;
            }

            GridViewRow gridViewRow = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
            string dispatchMissileID = gridView.DataKeys[gridViewRow.RowIndex]["DispatchMissileID"].ToString();

            switch (e.CommandName)
            {
                case "CmdSelect":

                    string returnValue = gridViewRow.Cells[0].Text 
                        + "|" + gridViewRow.Cells[1].Text 
                        + "|" + gridViewRow.Cells[2].Text
                        + "|" + gridViewRow.Cells[3].Text
                        + "|" + gridViewRow.Cells[4].Text
                        + "|" + gridViewRow.Cells[5].Text
                        + "|" + gridViewRow.Cells[6].Text
                        + "|" + dispatchMissileID;
                    Response.Write("<script>window.returnValue='" + returnValue + "';window.opener = null;window.open('', '_self', '');window.close();</script>");
                    break;
                default:
                    break;
            }
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow)
                return;

            string missileModel = gridView.DataKeys[e.Row.RowIndex]["MissileModel"].ToString();
            string missileCode = gridView.DataKeys[e.Row.RowIndex]["MissileCode"].ToString();
            DataTable dataTable = SBODBHelper.GetOITMInfo(missileModel);

            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                e.Row.Cells[0].Text = dataTable.Rows[0]["ItemName"].ToString();
                e.Row.Cells[3].Text = dataTable.Rows[0]["InvntryUom"].ToString();
            }

            e.Row.Cells[5].Text = SBODBHelper.GetCardName(missileModel, missileCode);
        }
    }
}